/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLogic;

import DTO.Booth;
import DTO.Exhibitor;
import DTO.ImportantDate;
import DTO.Product;
import DataAccess.DataUtility;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tran anh
 * @version 1.0
 */
public class ExhibitorManagement {

    /**
     * list exhibitor used to contain exhibitors from database
     */
    private Vector<Exhibitor> listExhibitor;

    /**
     * list ImportantDate used to contain important date from database.
     * every exhibitor has a important date
     */
    private Vector<ImportantDate> listImportDate;

    public ExhibitorManagement() {
    }

    /**
     * Load list Exhibitor from database
     * @return list Exhibitor
     */
    public Vector<Exhibitor> loadExhibitor() {
        try {
            listExhibitor = new Vector<Exhibitor>();

            ResultSet rs = DataUtility.excuteQuery("Select * from Exhibitor");
            while (rs.next()) {
                Exhibitor exhibitor = new Exhibitor();
                listExhibitor.add(exhibitor);

                exhibitor.setId(rs.getString("id"));
                exhibitor.setName(rs.getString("Name"));
                exhibitor.setAddress(rs.getString("address"));
                exhibitor.setComfirm(rs.getInt("Confirm"));
                exhibitor.setCompany(rs.getString("Company"));
                exhibitor.setEmail(rs.getString("email"));
                exhibitor.setFax(rs.getString("fax"));
                exhibitor.setMoney(rs.getFloat("money"));
                exhibitor.setPaidStatus(rs.getInt("paidStatus"));
                exhibitor.setPhone(rs.getString("Phone"));
                exhibitor.setStatus(rs.getInt("status"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listExhibitor;
    }

    /**
     * Check in database has a same nameExhibitor? if has return false
     * else return true
     * @param nameExhibitor
     * @return a boolean
     */
    public boolean checkName(String nameExhibitor){
        try {
            ResultSet rs = DataUtility.excuteQuery("select count(*) from Exhibitor where name = " + "\'" + nameExhibitor + "\'");
            if (rs.next()) {
                int count = rs.getInt(1);
                if(count > 0)
                    return false;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return true;
    }

    /**
     * Load list Important date from database, every object important date must be has
     * status = 1
     */
    public void loadImportDate() {
        try {
            listImportDate = new Vector<ImportantDate>();
            
            ResultSet rs = DataUtility.excuteQuery("Select * from ImportantDate where status = 1");
            while (rs.next()) {
                ImportantDate importDate = new ImportantDate();
                listImportDate.add(importDate);
                
                importDate.setContReturnDate(rs.getString("ContReturnDate"));
                importDate.setContSendDate(rs.getString("ContSendDate"));
                importDate.setIdExhibitor(rs.getString("IdExhibitor"));
                importDate.setLastChangeDate(rs.getString("LastChangeDate"));
                importDate.setPaidDate(rs.getString("PaidDate"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Generate new ID of Exhibitor and not same other Id has in database
     * @return String new ID
     */
    public String createID() {
        String id = "EX";
        int amount = 0;
        try {
            ResultSet rs = DataUtility.excuteQuery("select count(*) from Exhibitor");
            rs.next();
            amount = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String number = ""+ (amount + 1);
        if(number.length() == 1)
            id = id + "000";
        else if(number.length() == 2)
            id = id + "00";
        else if(number.length() == 3)
            id = id + "0";
        id = id + number;
        return id;
    }

    /**
     * Return a Exhibitor folow by it's index in array
     * @param index is specify location of it in array
     * @return a object Exhibitor
     */
    public Exhibitor getByIndex(int index) {
        return listExhibitor.get(index);
    }

    /**
     * Count number object important date in array
     * @return length of array
     */
    public int countListDate(){
        return listImportDate.size();
    }

    /**
     * Get a object important date folow by idExhibitor
     * @param idExhibitor is specify object Important date in array
     * @return a object important date
     */
    public ImportantDate getbyID(String idExhibitor) {
        for (ImportantDate im : listImportDate) {
            if (im.getIdExhibitor().equalsIgnoreCase(idExhibitor)) {
                return im;
            }
        }
        return null;
    }

    /**
     * Get information of object Exhibitor put into HashMap.
     * Then call method executeProcedure to insert object into database
     * @param exhibitor is new Object to get information pass into database
     */
    public void insert(Exhibitor exhibitor) {
        HashMap fiels = new HashMap();
        fiels.put("id", exhibitor.getId());
        fiels.put("address", exhibitor.getAddress());
        fiels.put("confirm", exhibitor.getComfirm());
        fiels.put("company", exhibitor.getCompany());
        fiels.put("status", exhibitor.getStatus());
        fiels.put("email", exhibitor.getEmail());
        fiels.put("fax", exhibitor.getFax());
        fiels.put("money", exhibitor.getMoney());
        fiels.put("name", exhibitor.getName());
        fiels.put("paidStatus", exhibitor.getPaidStatus());
        fiels.put("phone", exhibitor.getPhone());

        DataUtility.executeProcedure("insertExhibitor", fiels);
    }

    /**
     * Get information of object Important date put into HashMap.
     * Then call method executeProcedure to insert object into database.
     * every exhibitor has a important date, this method be called simultaneously
     * with method insert exhibitor.
     * @param im is new Object to get information pass into database
     * @see #insert(DTO.Exhibitor) 
     */
    public void insert(ImportantDate im) {
        HashMap fiels = new HashMap();
        fiels.put("ContReturnDate", im.getContReturnDate());
        fiels.put("ContSendDate", im.getContSendDate());
        fiels.put("PaidDate", im.getPaidDate());
        fiels.put("LastChangeDate", im.getLastChangeDate());
        fiels.put("IdExhibitor", im.getIdExhibitor());

        DataUtility.executeProcedure("insertImportantDate", fiels);
    }

    
    /**
     * Pay money of Exhibitor based on idExhibitor to identify Exhibitor
     * Payment. when a exhibior payment, paid date will be store
     * @param idExhibitor is String to specify Exhibitor
     * @param paidDate
     */
    public void paidMoney(String idExhibitor, String paidDate){
        HashMap fiels = new HashMap();
        fiels.put("idExhibitor", idExhibitor);
        fiels.put("Date", paidDate);

        DataUtility.executeProcedure("paidMoney", fiels);
    }

    /**
     * Get information of object Exhibitor put into HashMap.
     * Then call method executeProcedure to update object into database
     * @param exhibitor is new Object to get information pass into database
     */
    public void update(Exhibitor exhibitor) {
        HashMap fiels = new HashMap();
        fiels.put("id", exhibitor.getId());
        fiels.put("address", exhibitor.getAddress());
        fiels.put("confirm", exhibitor.getComfirm());
        fiels.put("company", exhibitor.getCompany());
        fiels.put("status", exhibitor.getStatus());
        fiels.put("email", exhibitor.getEmail());
        fiels.put("fax", exhibitor.getFax());
        fiels.put("money", exhibitor.getMoney());
        fiels.put("name", exhibitor.getName());
        fiels.put("paidStatus", exhibitor.getPaidStatus());
        fiels.put("phone", exhibitor.getPhone());

        DataUtility.executeProcedure("updateExhibitor", fiels);

    }

    /**
     * Get information of object Important date put into HashMap.
     * Then call method executeProcedure to update object into database
     * @param im is new Object to get information pass into database
     */
    public void update(ImportantDate im) {
        HashMap fiels = new HashMap();
        fiels.put("ContReturnDate", im.getContReturnDate());
        fiels.put("ContSendDate", im.getContSendDate());
        fiels.put("PaidDate", im.getPaidDate());
        fiels.put("LastChangeDate", im.getLastChangeDate());
        fiels.put("IdExhibitor", im.getIdExhibitor());

        DataUtility.executeProcedure("updateImportantDate", fiels);
    }


    /**
     * Get list year of Exhibitor was  join into App
     * @param idExhibitor is specify a Exhibitor
     * @return list year
     */
    public Vector<String> YearOfEx(String idExhibitor){
        Vector<String> listYear = new Vector<String>();

         try {
            HashMap fiels = new HashMap();
            fiels.put("idExhibitor", idExhibitor);

            ResultSet rs = DataUtility.executeProcedure("YearOfEx", fiels, true);
            
            while (rs.next()) {
                String year = rs.getString("year");
                listYear.add(year);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listYear;
    }

    /**
     * Get number booth by year of every Exhibitor
     * @param idExhibitor
     * @param year is specify year booth be created
     * @return list booth of a Exhibitor
     */
    public Vector<Booth> BoothByYear(String idExhibitor, String year){
        Vector<Booth> listBooth = new Vector<Booth>();

         try {
             HashMap fiels = new HashMap();
            fiels.put("year", year);
            fiels.put("idExhibitor", idExhibitor);

            ResultSet rs = DataUtility.executeProcedure("BoothbyYear", fiels, true);
            
            while (rs.next()) {
                Booth booth = new Booth();
                listBooth.add(booth);

                booth.setDateCreate(rs.getString("dateCreate"));
                booth.setId(rs.getString("id"));
                booth.setIdExhibitor(rs.getString("idExhibitor"));
                booth.setIdZone(rs.getString("idZone"));
                booth.setMoneyService(rs.getFloat("moneyService"));
                booth.setName(rs.getString("name"));
                booth.setNumberStaff(rs.getInt("numberStaff"));
                booth.setPriority(rs.getInt("priority"));
                booth.setSponsor(rs.getString("sponsor"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listBooth;
    }

    /**
     * Get number product by year of every Exhibitor
     * @param idExhibitor
     * @param year is specify year booth be created
     * @return list product of a Exhibitor
     */
    public Vector<Product> ProductByYear(String idExhibitor, String year){
        Vector<Product> listProduct = new Vector<Product>();

         try {
             HashMap fiels = new HashMap();
            fiels.put("year", year);
            fiels.put("idExhibitor", idExhibitor);

            ResultSet rs = DataUtility.executeProcedure("ProductbyYear", fiels, true);

            while (rs.next()) {
                Product product = new Product();
                listProduct.add(product);

                product.setDateCreate(rs.getString("DateCreate"));
                product.setDecription(rs.getString("decription"));
                product.setId(rs.getString("id"));
                product.setIdExhibitor(rs.getString("idExhibitor"));
                product.setName(rs.getString("Name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listProduct;
    }
}
